import type { MiddlewareHandler } from 'hono'
import { config } from '@/config/env'

// 请求日志中间件
export const requestLogger = (): MiddlewareHandler => {
  return async (c, next) => {
    const start = Date.now()
    const requestId = Math.random().toString(36).substring(2, 15)
    
    // 设置请求ID到上下文
    c.set('requestId', requestId)
    c.header('X-Request-ID', requestId)

    // 记录请求开始
    if (config.monitoring.logLevel === 'debug') {
      console.log(`[${requestId}] ${c.req.method} ${c.req.path} - Request started`)
    }

    await next()

    // 记录请求完成
    const duration = Date.now() - start
    const status = c.res.status
    
    console.log(`[${requestId}] ${c.req.method} ${c.req.path} - ${status} - ${duration}ms`)
  }
}
